package com.mystudy.leetcode.problem.sort.p_242;

import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * @program: infoalgorithm
 * @description: 有效的字母异位词
 * @author: zhouzhilong
 * @create: 2019-08-01 17:24
 **/
public class Solution {
    /** logger */
    private static final Logger LOGGER = LoggerFactory.getLogger(Solution.class);
    @Test
    public void test(){
        boolean anagram2 = isAnagram2("ab", "a");
        LOGGER.debug("anagram2 = [{}]",anagram2);
    }


    /**
     * 数组计数法
     * @param s
     * @param t
     * @return
     */
    public boolean isAnagram2(String s, String t) {
        if (s.length() != t.length()) {
            return false;
        }
        int[] table = new int[26];
        for (int i = 0; i < s.length(); i++) {
            table[s.charAt(i) - 'a']++;
        }
        for (int i = 0; i < t.length(); i++) {
            table[t.charAt(i) - 'a']--;
            if (table[t.charAt(i) - 'a'] < 0) {
                return false;
            }
        }
        return true;
    }

}
